﻿#include "ElementValue.h"
#include "DatabaseMgmt.h"


CElementValue::CElementValue()
{

}

CElementValue::~CElementValue()
{

}

int CElementValue::queryByReportUid(uint32_t report_id, QVector<ElementValue>& dataes)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT UID,REPORT_UID,ELEMENT_UID,ELEMENT"
		" FROM TB_ELEMENT_VALUE WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}
	else
	{
		while (query.next())
		{
			ElementValue data;
			data.uid = query.value("UID").toUInt();
			data.report_uid = query.value("REPORT_UID").toUInt();
			data.element_uid = query.value("ELEMENT_UID").toUInt();
			data.element = query.value("ELEMENT").toDouble();
			dataes.append(data);
		}
	}

	return ret;
}

int CElementValue::queryViewByReportUid(uint32_t report_id, QVector<ElementViewValue>& dataes)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT VALUE_ID,REPORT_ID,ELEMENT_VALUE,ELEMENT_UID,ELEMENT_NAME,"
		" MIN_VALUE, MAX_VALUE FROM VIEW_ELEMENT_VALUE WHERE REPORT_ID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}
	else
	{
		while (query.next())
		{
			ElementViewValue data;
			data.value_id = query.value("VALUE_ID").toUInt();
			data.report_id = query.value("REPORT_ID").toUInt();
			data.element_value = query.value("ELEMENT_VALUE").toDouble();
			data.element_uid = query.value("ELEMENT_UID").toUInt();
			data.element_name = query.value("ELEMENT_NAME").toString();
			data.min_value = query.value("MIN_VALUE").toDouble();
			data.max_value = query.value("MAX_VALUE").toDouble();
			dataes.append(data);
		}
	}

	return ret;
}

int CElementValue::removeByReportUid(uint32_t report_id)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_ELEMENT_VALUE WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CElementValue::modifyByUid(ElementValue data)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("UPDATE TB_ELEMENT_VALUE SET REPORT_UID=%1,ELEMENT_UID=%2,ELEMENT=%3,"
		" WHERE UID=%4")
		.arg(data.report_uid)
		.arg(data.element_uid)
		.arg(data.element)
		.arg(data.uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CElementValue::insertRow(ElementValue data)
{
	int ret = 0;

	QString strSql = QString("INSERT INTO TB_ELEMENT_VALUE (REPORT_UID,ELEMENT_UID,ELEMENT)"
		" VALUES (%1,%2,%3)")
		.arg(data.report_uid)
		.arg(data.element_uid)
		.arg(data.element);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

